<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript">

	$(function() {
		$('#moduls_permission_treegrid').treegrid({
			url : '${pageContext.request.contextPath}/permissionAction!query.action',
			idField : 'id',
			treeField : 'text',
			parentField : 'pid',
			fit : true,
			fitColumns : false,
			//rownumbers : true,
			nowrap : false,
			border : false,
			columns : [ [ {
				title : '编号',
				field : 'id',
				width : 150,
				checkbox : true,
				hidden : true
			}, {
				title : '模块名',
				field : 'text',
				width : 250
			}, {
				title : '模块地址',
				field : 'url',
				width : 320,
				formatter : function(value, row, index) {
					if (row.permValue) {
						return value;
					} else {
						return '';
					}
				}
			}, {
				title : '权限值',
				field : 'permValue',
				width : 50
			}, {
				field : 'action',
				title : '动作',
				width : 60,
				formatter : function(value, row, index) {
					if (row.permValue) {
						return formatString('<img onclick="admin_permission_editFun(\'{0}\');" src="{1}"/>&nbsp;&nbsp;&nbsp;' + '<img onclick="admin_permission_deleteFun(\'{2}\');" src="{3}"/>', row.id, 'jslib/jquery-easyui-1.3.1/themes/icons/pencil.png', row.id, 'jslib/jquery-easyui-1.3.1/themes/icons/cancel.png');
					} else {
						return '';
					}
				}
			} ] ],
			toolbar : [ {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					admin_permission_appendFun();
				}
			}, '-', {
				text : '展开',
				iconCls : 'icon-redo',
				handler : function() {
					var node = $('#moduls_permission_treegrid').treegrid('getSelected');
					if (node) {
						$('#moduls_permission_treegrid').treegrid('expandAll', node.cid);
					} else {
						$('#moduls_permission_treegrid').treegrid('expandAll');
					}
				}
			}, '-', {
				text : '折叠',
				iconCls : 'icon-undo',
				handler : function() {
					var node = $('#moduls_permission_treegrid').treegrid('getSelected');
					if (node) {
						$('#moduls_permission_treegrid').treegrid('collapseAll', node.cid);
					} else {
						$('#moduls_permission_treegrid').treegrid('collapseAll');
					}
				}
			}, '-', {
				text : '权限分配',
				iconCls : 'icon-remove',
				handler : function() {
					moduls_permission_distributeFun();
				}
			}, '-', {
				text : '刷新',
				iconCls : 'icon-reload',
				handler : function() {
					$('#moduls_permission_treegrid').treegrid('reload');
				}
			} ],
			onContextMenu : function(e, row) {
				e.preventDefault();
				$(this).treegrid('unselectAll');
				$(this).treegrid('select', row.id);
				$('#admin_permission_menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
	});

	function admin_permission_appendFun() {
		$('#admin_permission_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/moduls/fun/addPermission.jsp',
			width : 500,
			height : 200,
			modal : true,
			title : '添加权限',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#moduls_fun_addPermission_addForm').form('submit', {
						url : '${pageContext.request.contextPath}/permissionAction!add.action',
						success : function(result) {
							var r = $.parseJSON(result);
							if (r.success) {
								$('#moduls_permission_treegrid').treegrid('reload');
								d.dialog('destroy');
							}
							$.messager.show({
								title : '提示',
								msg : r.msg
							});
						}
					});
				}
			}, {
				text : '取消',
				iconCls : 'icon-remove',
				handler : function() {
					$(this).closest('.window-body').dialog('destroy');
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			}
		});
	}
	function admin_permission_editFun(id) {
		if (id != undefined) {
			$('#moduls_permission_treegrid').treegrid('select', id);
		}
		var node = $('#moduls_permission_treegrid').treegrid('getSelected');
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/moduls/fun/editPermission.jsp',
			width : 500,
			height : 200,
			modal : true,
			title : '权限编辑',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#moduls_fun_editPermission_editForm').form('submit', {
						url : '${pageContext.request.contextPath}/permissionAction!modify.action',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									$('#moduls_permission_treegrid').treegrid('reload');
									d.dialog('destroy');
								}
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			}, {
				text : '取消',
				iconCls : 'icon-remove',
				handler : function() {
					$(this).closest('.window-body').dialog('destroy');
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			},
			onLoad : function() {
				$('#moduls_fun_editPermission_editForm').form('load', node);
			}
		});
	}

	var permData;
	function moduls_permission_distributeFun(id) {
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/moduls/fun/distributePermission.jsp',
			width : 400,
			height : 150,
			modal : true,
			title : '权限分配',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					var d = $(this).closest('.window-body');

					var ids = $('#moduls_fun_distributePerm_permId').combotree('getValues');
					var permTotalValue = getPermTotalValue(ids, permData);

					$('#moduls_fun_distributePermission_distributeForm').form('submit', {
						url : '${pageContext.request.contextPath}/permissionAction!distribute.action?PermValue=' + permTotalValue,
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									$('#moduls_permission_treegrid').datagrid('load');
									d.dialog('destroy');
								}
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			}, {
				text : '取消',
				iconCls : 'icon-remove',
				handler : function() {
					$(this).closest('.window-body').dialog('destroy');
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			},
			onLoad : function() {
				$('#moduls_fun_distributePermission_roleId').combogrid({
					url : '${pageContext.request.contextPath}/permissionAction!queryRole.action',
					loadMsg : '数据加载中....',
					missingMessage : '请检索用户名',
					panelWidth : 420,
					panelHeight : 180,
					delay : 500,
					required : true,
					nowrap : false,
					fitColumns : false,
					value : '',
					idField : 'roleId',
					textField : 'roleName',
					mode : 'remote',
					pagination : true,
					pageSize : 5,
					pageList : [ 5, 10 ],
					columns : [ [ {
						title : '编号',
						field : 'roleId',
						width : 150,
						hidden : true
					}, {
						title : '角色名',
						field : 'roleName',
						width : 120
					}, {
						title : '权限名',
						field : 'ids',
						width : 280,
						formatter : function(value, row, index) {
							//if (value == 0)
							//return '';
							return row.permNames;
						}
					} ] ],
					onHidePanel : function() {
						////console.info($(this).combogrid("getValue"));
					},
					onSelect :function(rowIndex, rowData) {
						//console.info(rowData);
						$('#moduls_fun_distributePerm_permId').combotree('setValues', stringToList(rowData.ids));
					}
				});
				
				$('#moduls_fun_distributePerm_permId').combotree({
					url : '${pageContext.request.contextPath}/permissionAction!query.action',
					parentField : 'pid',
					multiple : true,
					lines : true,
					onLoadSuccess :function(node, data) {
						permData = data;
					}
				});
			}
		});
	}

	function admin_permission_deleteFun(id) {
		if (id != undefined) {
			$('#moduls_permission_treegrid').treegrid('select', id);
		}
		var node = $('#moduls_permission_treegrid').treegrid('getSelected');
		if (node) {
			$.messager.confirm('询问', '您确定要删除【' + node.text + '】？', function(b) {
				if (b) {
					$.ajax({
						url : '${pageContext.request.contextPath}/permissionAction!delete.action',
						data : {
							id : node.id
						},
						cache : false,
						dataType : 'JSON',
						success : function(r) {
							if (r.success) {
								//$('#layout_west_menuTree').tree('reload');
								$('#moduls_permission_treegrid').treegrid('reload');
							}
							$.messager.show({
								msg : r.msg,
								title : '提示'
							});
						}
					});
				}
			});
		}
	}

	var permList = [];
	function getPermTotalValue(ids, permData) {
		permList = [];
		permTotalValue = 0;
		recursionPermData(permData);
		var permTotalValue = 0;
		for ( var i = 0; i < permList.length; i++) {
			for ( var j = 0; j < ids.length; j++) {
				if (permList[i].id == ids[j]) {
					permTotalValue += Math.pow(2, permList[i].permValue);
				}
			}
		}
		return permTotalValue;
	}

	function recursionPermData(permData) {
		for ( var i = 0; i < permData.length; i++) {
			if (permData[i].children) {
				recursionPermData(permData[i].children);
			} else {
				if (permData[i].permValue) {
					permList.push(permData[i]);
					////console.info('id:' + permData[i].id + ' value:' + permData[i].permValue);
				}
			}
		}
	}
</script>
<table id="moduls_permission_treegrid"></table>
<div id="admin_permission_menu" class="easyui-menu" style="width:120px;display: none;">
	<div onclick="admin_permission_appendFun();" data-options="iconCls:'icon-add'">增加</div>
	<div onclick="admin_permission_deleteFun();" data-options="iconCls:'icon-remove'">删除</div>
	<div onclick="admin_permission_editFun();" data-options="iconCls:'icon-edit'">编辑</div>
</div>